package com.lingjtx.common.redis.stream;

import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Component
public class RedisEventListenerRegistry {

    private final Map<Class<? extends RedisStreamTypedEvent>, List<RedisEventListener<?>>> listenerMap = new ConcurrentHashMap<>();

    public <T extends RedisStreamTypedEvent> void register(RedisEventListener<T> listener) {
        listenerMap
                .computeIfAbsent(listener.getEventType(), k -> new ArrayList<>())
                .add(listener);
    }

    public List<RedisEventListener<?>> getListeners(Class<? extends RedisStreamTypedEvent> type) {
        return listenerMap.getOrDefault(type, Collections.emptyList());
    }
}
